﻿using System;
using System.Collections.Generic;
using System.Windows.Ria;

namespace SoftSource.Ria
{
    /// <summary>
    /// Wraps an EntityWrapper, but exposes read-only information
    /// </summary>
    /// <typeparam name="T">The type of Entity being wrapped</typeparam>
    public sealed class ReadOnlyEntityWrapper<T> : IObservable<Unit> where T : Entity, new()
    {
        private EntityWrapper<T> wrapper;

        public ReadOnlyEntityWrapper(DomainContext context)
            : this(context, null)
        {
        }

        public ReadOnlyEntityWrapper(DomainContext context, Func<EntityQuery<T>> defaultLoadQuery)
        {
            EntityWrapper<T> wrapper = new EntityWrapper<T>(context, defaultLoadQuery);
            this.wrapper = wrapper;
        }

        public ReadOnlyEntityWrapper(EntityWrapper<T> wrapper)
        {
            this.wrapper = wrapper;
        }

        /// <summary>
        /// The entities stored in the underlying EntitySet
        /// </summary>
        public IEnumerable<T> Entities
        {
            get { return this.wrapper.Entities; }
        }

        /// <summary>
        /// The default load query
        /// </summary>
        public IObservable<Unit> Refresh
        {
            get { return this.wrapper.Refresh; }
        }

        /// <summary>
        /// The default load query that can only be executed once.
        /// </summary>
        public IObservable<Unit> Load
        {
            get { return this.wrapper.Load; }
        }

        #region IObservable<Unit> Members

        public IDisposable Subscribe(IObserver<Unit> observer)
        {
            return this.wrapper.Subscribe(observer);
        }

        #endregion
    }
}
